class BulletNode extends CoolNode
{
    public constructor(callback:Function, obj:egret.DisplayObjectContainer) 
	{
		super();
		this.m_Callback = callback;
		this.m_callbackObj = obj;
		this.init();
	}
	private m_Bullet:egret.Bitmap;
	private m_BulletBg:egret.Bitmap;
	private m_animation:egret.Tween;
	private m_Callback:Function;
	private m_callbackObj:egret.DisplayObjectContainer;
    private init()
    {
        this.initTouchBg();
		this.touchEnabled = true;
		this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
		this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMoved, this);
		this.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
		this.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.touchCancel, this);
		this.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.touchCancel, this);
    }
    private initTouchBg()
    {
        let touchMap:egret.Bitmap = this.createBitmapByName("black");
        touchMap.alpha = 0;
        this.addChild(touchMap);

		this.m_BulletBg = this.createBitmapByName("baxin2_png");
		this.m_BulletBg.anchorOffsetX = this.m_BulletBg.width / 2;
		this.m_BulletBg.anchorOffsetY = this.m_BulletBg.height / 2;
		this.m_BulletBg.visible = false;
		this.addChild(this.m_BulletBg);


		this.m_Bullet = this.createBitmapByName("b_0");
		this.m_Bullet.anchorOffsetX = this.m_Bullet.width / 2;
		this.m_Bullet.anchorOffsetY = this.m_Bullet.height / 2;
		this.m_Bullet.visible = false;
		this.addChild(this.m_Bullet);
    }
	public stopAction()
	{
		if(this.m_animation != null)
		{
			egret.Tween.pauseTweens(this.m_Bullet);
		}
	}
    private touchBegin(event:egret.TouchEvent)
	{
		this.touchEnabled = false;
		this.m_Bullet.visible = true;
		this.m_Bullet.scaleX = 1;
		this.m_Bullet.scaleY = 1;
		this.m_Bullet.x = event.stageX;
		this.m_Bullet.y = 640;//event.stageY;
		this.m_BulletBg.x = event.stageX - this.m_BulletBg.width * 0.3 - 12;
		this.m_BulletBg.y = event.stageY - this.m_BulletBg.height * 0.3 - 5;

		// var shp:egret.Shape = new egret.Shape();
		// shp.graphics.beginFill( 0xff0000 );
		// shp.graphics.drawRect(this.m_BulletBg.x,this.m_BulletBg.y,100,100);
		// shp.graphics.endFill();
		// shp.width = 100;
		// shp.height = 100;
		// this.addChild( shp )

		// this.m_animation = egret.Tween.get(this.m_Bullet).to({ "y":event.stageY}, 600).call(()=>
		// {
		// 	if(this.m_Callback != null)
		// 	{
		// 		let bindFunction = this.m_Callback.bind(this.m_callbackObj);
		// 		bindFunction(this.m_BulletBg.x,this.m_BulletBg.y);
		// 	}

		// });

		this.picTag = 0;
		this.picTime = 0;
		this.yPos = (this.m_Bullet.y - event.stageY) / 29;
		egret.startTick(this.picAnimation,this);
	}
	private yPos:number;
	private picAnimation(delay:number):boolean
	{
		this.m_Bullet.y -= this.yPos;
		if(this.picTime%4==0)
		{
			let pic:string = "b_" + this.picTag;
			this.m_Bullet.texture = RES.getRes(pic);
			this.picTag++;
		}
		this.picTime++;
		if(this.picTag > 7)
		{
			egret.stopTick(this.picAnimation,this);
			if(this.m_Callback != null)
			{
				let bindFunction = this.m_Callback.bind(this.m_callbackObj);
				bindFunction(this.m_BulletBg.x,this.m_BulletBg.y);
			}
		}
		return false;
	}
	private picTag:number = 0;
	private picTime:number = 0;

	public OnClicked(piece:ContentPiece)
	{
		this.m_BulletBg.visible = true;
		this.m_BulletBg.scaleX = 0.4;
		this.m_BulletBg.scaleY = 0.4;
		egret.Tween.get(this.m_BulletBg).to({}, 200).call(()=>
		{
			piece.setDisable();
			this.m_BulletBg.visible = false;
			this.m_Bullet.visible = false;
			this.touchEnabled = true;
		})
	}
	public OnUnClicked()
	{
		this.m_BulletBg.visible = false;
		this.m_Bullet.visible = false;
		this.touchEnabled = true;
	}

	private touchMoved(event:egret.TouchEvent)
	{

	}
	private touchEnd(event:egret.TouchEvent)
	{

	}
    private touchCancel(event:egret.TouchEvent)
	{

	}
}